<?php
/**
 * User: hufeng
 * Date: 2016/12/18
 * Desc: 游戏类型Model
 */

namespace Model;
use Think\Model;

class UserAssetsModel extends Model{
    protected $connection = 'DB_SERVE';
    protected $tableName = 'user_assets';
    //查询单条记录
    public function findData($where){
        $re = $this->where($where)->find();
        return $re;
    }
    //查询多条记录
    public function selectData($where,$fields='*'){
        $re = $this->field($fields)->where($where)->select();
        return $re;
    }
    //更新记录
    public function editData($where,$data){
        $re = $this->where($where)->save($data);
        return $re;
    }
    //添加
    public function addData($data){
        $re = $this->add($data);
        return $re;
    }
    /*筛选数据*/
    public function search($param,$page,$pernum,$sortStr='id desc',$exportxls=false){
        if($exportxls){
            $res = $this->where($param)->order($sortStr)->select();
        }else{
            $res = $this->where($param)->order($sortStr)->limit($pernum)->page($page)->select();
        }
        return $res;
    }
    public function getTotalNum($param){
        return $this->where($param)->count();
    }
    public function sumData($field,$where){
        return $this->where($where)->sum($field);
    }
    //用户可用资产
    public function userBalance($uid){
        $where['uid'] = $uid;
        $where['status'] = 1;
        $list = $this->selectData($where);
        $data = [];
        foreach ($list as $val){
            $data[$val['type']] = $val['money'];
        }
        return $data;
    }

    //增加用户资产
    public function addAsset($uid,$money,$type,$status,$memo='管理后台充值'){
        $res = $this->findData(['uid'=>$uid,'type'=>$type,'status'=>$status]);
        $leftMoney = $money;
        if($res){
            $leftMoney = round($res['money']+$money,2);
            $this->editData(['id'=>$res['id']],['money'=>$leftMoney]);
        }else{
            $this->addData(['uid'=>$uid,'type'=>$type,'status'=>$status,'money'=>$leftMoney]);
        }
        $ualModel = new UserAssetsLogModel();
        $ualModel->addData([
            'uid'=>$uid,
            'change_money'=>$money,
            'left_money'=>$leftMoney,
            'type'=>$type,
            'status'=>$status,
            'addtime'=>date('Y-m-d H:i:s'),
            'memo'=>$memo,
        ]);
        return ['code'=>0,'money'=>$leftMoney];
    }
    //扣除用户资产
    public function reducAsset($uid,$money,$type,$status,$memo='管理后台扣款'){
        $res = $this->findData(['uid'=>$uid,'type'=>$type,'status'=>$status]);
        if(round($res['money'],2)<round($money,2)){
            return ['code'=>1,'money'=>0];
        }
        $leftMoney = round($res['money']-$money,2);
        $this->editData(['id'=>$res['id']],['money'=>$leftMoney]);

        $ualModel = new UserAssetsLogModel();
        $ualModel->addData([
            'uid'=>$uid,
            'change_money'=>-$money,
            'left_money'=>$leftMoney,
            'type'=>$type,
            'status'=>$status,
            'addtime'=>date('Y-m-d H:i:s'),
            'memo'=>$memo,
        ]);
        return ['code'=>0,'money'=>$leftMoney];
    }
}